<?php

namespace app\saas\service;

use app\common\model\trResource;
use app\common\util\ErrorCode;
use app\saas\model\trSetting;

/**
 * Class Auth
 * @package app\admin\service
 * 设置服务类
 */
class SettingService
{
    private $params;
    private $returnData;
    private $field = [
        "base" => ["logo" => null, "site_title" => null, "seo_title" => null, "copyright" => null],
        "sms" => ["sms_id" => null, "sms_secret" => null, "sms_sign" => null, "sms_code" => null],
        "tencent" => ["secret_id" => null, "secret_key" => null, "bucket" => null, "region" => null],
        "wxOpen" => ["open_app_id" => null, "open_app_secret" => null, "msg_verify_token" => null, "encoding_key" => null],
    ]; //填入数据库需要存储的表字段
    private $fieldMsg = [
        "logo" => "logo", "site_title" => "站点标题", "seo_title" => "网站seo的标题", "copyright" => "版权信息", "sms_id" => "短信id",
        "sms_secret" => "短信secret", "sms_sign" => "短信签名", "sms_code" => "验证码模板ID", "secret_id" => "访问密钥ID",
        "secret_key" => "访问密钥key", "bucket" => "存储桶名", "region" => "地域",
        "open_app_secret" => '第三方平台appSecret', "msg_verify_token" => '消息校验Token', "encoding_key" => '消息加解密Key'
    ];
    private $data = [];

    public function __construct($data, $params)
    {
        $this->returnData = $data;
        $this->params = $params;
        $this->field["tencent"]["secret_id"] = config("thirdConfig.QCloudConfig.secretId");
        $this->field["tencent"]["secret_key"] = config("thirdConfig.QCloudConfig.secretKey");
        $this->field["tencent"]["bucket"] = config("thirdConfig.QCloudConfig.bucket");
        $this->field["tencent"]["region"] = config("thirdConfig.QCloudConfig.region");
    }

    public function getSettingInfo(){
        if(request()->manageData["type"] != "1"){
            return ErrorCode::code("customized", "您无权限操作");
        }
        $settingData = trSetting::getFields("*");
        if(!empty($settingData)){
            $settingData["logoUrl"] = tome_dia(trResource::getValue("url", ["id" => $settingData["logo"]]));
        }else{
            $settingData["logoUrl"] = "";
        }
        $data["base"]["logoUrl"] = $settingData["logoUrl"];
        foreach ($this->field as $k => $v){
            foreach ($v as $field => $i){
                $field = parse_name($field);
                $data[$k][$field] = isset($settingData[$field]) ? $settingData[$field] : $i;
            }
        }
        $data["wxOpen"]["authEventUrl"] = request()->domain().request()->server("SCRIPT_NAME").'/admin/wxEvent/message/$APPID$';
        $data["wxOpen"]["msgEventUrl"] = request()->domain().request()->server("SCRIPT_NAME")."/admin/wxEvent/auth";
        $this->returnData["data"] = $data;
        return $this->returnData;
    }

    public function toSaveSettingInfo(){
        if(request()->manageData["type"] != "1"){
            return ErrorCode::code("customized", "您无权限操作");
        }
        if(empty($this->params["type"])){
            return ErrorCode::code("params_not_exist", "type");
        }
        if(!isset($this->field[$this->params["type"]])){
            return ErrorCode::code("params_error", "type");
        }
        foreach ($this->field[$this->params["type"]] as $k => $v) {
            $ks = parse_name($k); //转换字段，变驼峰
            if ($v === 'require' && !isset($this->params[$ks])) {
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            if (isset($this->params[$ks])) {
                $this->data[$k] = trim($this->params[$ks]);
            }
        }
        $settingData = trSetting::getFields("id");
        if(empty($settingData)){
            trSetting::create($this->data);
        }else{
            trSetting::update($this->data, ["id" => $settingData["id"]]);
        }
        $this->returnData["msg"] = "保存成功";
        return $this->returnData;
    }
}